package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsAdminMapper;
import com.shop.mapper.DtsBrandMapper;
import com.shop.pojo.DtsAdmin;
import com.shop.pojo.DtsBrand;
import com.shop.service.DtsAdminService;
import com.shop.util.bcrypt.BCryptPasswordEncoder;
import com.shop.vo.CategoryAssist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DtsAdminServiceImpl
 * @Author xiao宋
 * @Date 13:53  2022/10/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    DtsBrandMapper brandMapper;
    /**
     * 通过用户名查询用户。
     * @param username
     * @return 查询到的用户实体类
     */
    @Override
    public DtsAdmin findByUsername(String username) {
//      创建查询条件
        LambdaQueryWrapper<DtsAdmin>wrapper=new LambdaQueryWrapper<>();
//      用户名精确查询
        wrapper.eq(DtsAdmin::getUsername, username );
//        只需要返回一条数据，所以直接selectone
        return adminMapper.selectOne(wrapper);
//        return null;
    }
    public Map<String,Object> findByPage(Integer page,Integer limit,String sort,String order,String username){
        IPage <DtsAdmin>iPage=new Page<>(page,limit);
        QueryWrapper<DtsAdmin> w=new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            w.eq("username",username);
        }
//        根据传过来的 order数据判断是正序还是倒序
        if ("desc".equalsIgnoreCase(order)){
            w.orderByDesc(sort);
        }else {
            w.orderByAsc(sort);
        }
        adminMapper.selectPage(iPage,w);
//        前端显示用户列表，不应该把密码传过去
        List<DtsAdmin>adminList=iPage.getRecords();
        adminList=adminList.stream().map(admin -> {
            admin.setPassword("");
            return admin;
        }).collect(Collectors.toList());

        Map<String,Object>result=new HashMap<>();
        result.put("total", iPage.getTotal());
        result.put("items", iPage.getRecords());

        return result;
    }

    @Override
    public void delete(DtsAdmin admin) {
        LambdaQueryWrapper<DtsBrand>w=new LambdaQueryWrapper<>();
        w.eq(DtsBrand::getAdminId, admin.getId());
        w.eq(DtsBrand::getDeleted,false);
        Integer count= brandMapper.selectCount(w);
        if (count>0){
            throw new RuntimeException("改管理员被品牌关联，请先删除该品牌。");
        }
        admin.setDeleted(true);
        adminMapper.updateById(admin);
    }

    @Override
    public void update(DtsAdmin admin) {

        admin.setUpdateTime(new Date());
//        给密码加密
        BCryptPasswordEncoder b=new BCryptPasswordEncoder();
        String s = b.encode(admin.getPassword());
        admin.setPassword(s);
//        执行修改操作
        adminMapper.updateById(admin);

    }

    @Override
    public void insert(DtsAdmin admin) {
//      设置一些字段，前端不给传，我们自己产
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
//      给密码加密
        BCryptPasswordEncoder p=new BCryptPasswordEncoder();
        String s = p.encode(admin.getPassword());
        admin.setPassword(s);
//        执行新增
        adminMapper.insert(admin);
    }

    @Override
    public List<CategoryAssist> findAllAdmin() {
        List<DtsAdmin> adminList = adminMapper.selectList(null);
        List<CategoryAssist> list=new ArrayList<>();
        for (DtsAdmin admin : adminList) {
//            DtsAdmin a = new DtsAdmin();
//            a.setId(admin.getId());
//            a.setUsername(admin.getUsername());
            CategoryAssist ca = new CategoryAssist();
            ca.setValue(admin.getId());
            ca.setLabel(admin.getUsername());

            list.add(ca);

        }
        return list;
    }
}
